package com.ayoka.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.ayoka.R;
import com.ayoka.utils.AudioRecordManager;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by jinchunping on 2018/7/19.
 */

public class RecordAudioWaveView extends View {

    private int width, height, centerWidth, centerHeight;

    private Paint waveViewPaint;
    //矩形波纹颜色
    private int waveViewColor;
    //矩形波纹宽度
    private float waveViewWidth;
    //矩形波纹radius
    private float waveViewRadius;
    // 间距
    private float waveViewSpacing;
    private boolean isStart = false;

    private int LINE_W = 9;//默认矩形波纹的宽度，9像素, 原则上从layout的attr获得
    private int MIN_WAVE_H = 2;//最小的矩形线高，是线宽的2倍，线宽从lineWidth获得
    private int MAX_WAVE_H = 7;//最高波峰，是线宽的4倍
    private int current_wave_height = MIN_WAVE_H;

    //默认矩形波纹的高度，总共9个矩形，左右各有9个
    private int[] DEFAULT_WAVE_HEIGHT = {2, 3, 4, 3, 2, 4, 2, 2, 2};
    private LinkedList<Integer> mWaveList = new LinkedList<>();

    private RectF rectRight = new RectF();//右边波纹矩形的数据，10个矩形复用一个rectF
    private RectF rectLeft = new RectF();//左边波纹矩形的数据

    private LinkedList<Integer> list = new LinkedList<>();

    private static final int UPDATE_INTERVAL_TIME = 100;//100ms更新一次

    public RecordAudioWaveView(Context context) {
        super(context);
        init(context, null);
    }

    public RecordAudioWaveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RecordAudioWaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        resetList(list, DEFAULT_WAVE_HEIGHT);

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RecordAudioWaveView);
        waveViewColor = mTypedArray.getColor(R.styleable.RecordAudioWaveView_RWV_waveViewColor, Color.parseColor("#5182fc"));
        waveViewWidth = mTypedArray.getDimension(R.styleable.RecordAudioWaveView_RWV_waveViewWidth, LINE_W);
        mTypedArray.recycle();

        waveViewRadius = waveViewWidth / 2;

        waveViewPaint = new Paint();
        waveViewPaint.setAntiAlias(true);
        waveViewPaint.setColor(waveViewColor);
        waveViewPaint.setStyle(Paint.Style.FILL);
    }

    private void resetList(List list, int[] array) {
        list.clear();
        for (int i = 0; i < array.length; i++) {
            list.add(array[i]);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        centerWidth = width / 2;
        centerHeight = height / 2;
        float totalSpace = width - 2 * list.size() * waveViewWidth;
        waveViewSpacing = totalSpace / (2 * list.size() - 1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        // 设置中心的竖线
        RectF center = new RectF(centerWidth - waveViewWidth / 2, centerHeight - current_wave_height * waveViewWidth / 2, centerWidth + waveViewWidth / 2, centerHeight + current_wave_height * waveViewWidth / 2);
        canvas.drawRoundRect(center, waveViewRadius, waveViewRadius, waveViewPaint);

        for (int i = 0; i < DEFAULT_WAVE_HEIGHT.length; i++) {
            //右边矩形
            rectRight.left = centerWidth + 2 * i * waveViewWidth + waveViewSpacing + waveViewSpacing / 2;
            rectRight.top = centerHeight - list.get(i) * waveViewWidth / 2;
            rectRight.right = rectRight.left + waveViewWidth;
            rectRight.bottom = rectRight.top + list.get(i) * waveViewWidth;

            //左边矩形
            rectLeft.left = centerWidth - (2 * i * waveViewWidth + waveViewWidth + waveViewSpacing) - waveViewSpacing / 2;
            rectLeft.top = centerHeight - list.get(i) * waveViewWidth / 2;
            rectLeft.right = rectLeft.left + waveViewWidth;
            rectLeft.bottom = rectRight.top + list.get(i) * waveViewWidth;

            canvas.drawRoundRect(rectRight, waveViewRadius, waveViewRadius, waveViewPaint);
            canvas.drawRoundRect(rectLeft, waveViewRadius, waveViewRadius, waveViewPaint);
        }
        if (isStart) {
            refreshElement();
            postInvalidateDelayed(80);
        }
    }

    private synchronized void refreshElement() {
        float maxAmp = AudioRecordManager.getInstance().getMaxAmplitude();
        current_wave_height = MIN_WAVE_H + Math.round(maxAmp * (MAX_WAVE_H - 2));//wave 在 2 ~ 7 之间
        list.add(0, current_wave_height);
        list.removeLast();
    }

    public void start() {
        isStart = true;
        postInvalidate();
    }

    public void stop() {
        isStart = false;
        current_wave_height = MIN_WAVE_H;
        for (int i = 0; i < DEFAULT_WAVE_HEIGHT.length; i++) {
            list.set(i, MIN_WAVE_H);
        }
        postInvalidate();
    }
}